<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>使用者位置</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
    body,
    html,
    #container{
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        font-family: "寰蒋闆呴粦";
    }
    .info{
        z-index: 999;
        width: auto;
        min-width: 22rem;
        padding: .75rem 1.25rem;
        margin-left: 1.25rem;
        position: fixed;
        top: 1rem;
        background-color: #fff;
        border-radius: .25rem;
        font-size: 14px;
        color: #666;
        box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
    }
    </style>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=EzAgfFNdkPDSZxWfU8aoc516ZgcXj3MM"></script>
</head>
<body>

    <div id="container"></div>

    <script>
        lng=117.14648063708772;
        lat=39.06567016336429;
        var map = new BMapGL.Map('container'); //
        map.centerAndZoom(new BMapGL.Point( lng,lat), 20); //
        map.enableScrollWheelZoom(true); //
        // var marker;
        var last_point = new BMapGL.Point(lng, lat);
        var last_marker = new BMapGL.Marker(last_point)
        map.addOverlay(last_marker);
        var trackPoints = [];
        const MAX_POINTS = 50;

        function draw_trail(lng,lat){
            var t = new BMapGL.Point(lng,lat);
            var m = new BMapGL.Marker(t);
            if (t!=last_point){
             map.addOverlay(m);
             last_marker.remove();
            }


            var polyline = new BMapGL.Polyline([
                    last_point,
                    t
                ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});   //创建折线
            map.addOverlay(polyline);

            last_point = t;
        }

        function update() {
            fetch('gps_data.json')
                .then(res => {
                    if (!res.ok) throw new Error("HTTP error: " + res.status);
                    return res.json();
                })
                .then(data => {
                    console.log("new location:", data);

                    var newPoint = new BMapGL.Point(data.lng, data.lat);
                    marker = new BMapGL.Marker(newPoint);
                    // last_point = marker;
                    draw_trail(data.lng, data.lat);
                    //map.addOverlay(marker);
                    // marker.remove();
                    map.setCenter(newPoint);

                })
                .catch(err => console.error('数据获取失败:', err));
        }
        map.addEventListener('click', function (e) {
        alert('经度:' + e.latlng.lng + ',纬度:' + e.latlng.lat);
    });
        //
        setInterval(update, 3000);
        update(); //
    </script>
</body>
</html>
